package com.player.audio;

import java.io.IOException;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SongListActivity extends ListActivity {
	
	OdiohPlayaApplication application;
	OdiohMediaPlayer audioPlayer;
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		application = (OdiohPlayaApplication)getApplication();
		audioPlayer = application.getAudioPlayer();
		
		ContentResolver contentResolver = getContentResolver();
		Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
		Cursor cursor = contentResolver.query(uri, new String[] {
				android.provider.MediaStore.Audio.AudioColumns.DATA,
				android.provider.MediaStore.Audio.AudioColumns.TITLE }, null,
				null, null);

		if (cursor == null) {
			// query failed, handle error.
		} else if (!cursor.moveToFirst()) {
			// no media on the devices
		} else {

			int titleColumn = cursor
					.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
			int pathColumn = cursor
					.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);

			do {

				String songTitle = cursor.getString(titleColumn);
				String songPath = cursor.getString(pathColumn);
				audioPlayer.getSongTitleList().add(songTitle);
				audioPlayer.getSongPathList().add(songPath);

			} while (cursor.moveToNext());
		}

		setListAdapter(new ArrayAdapter<String>(this, R.layout.songs,
				audioPlayer.getSongTitleList()));

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				ArrayList<String> songTitleList = audioPlayer.getSongTitleList();
				
				if(songTitleList != null && songTitleList.size() > 0) {
					audioPlayer.setCurrentSongTitle(songTitleList.get(position));
				 
				}
				
				else {
					audioPlayer.setCurrentSongTitle("Empty song list");
				}

				try {

					if (audioPlayer.isPlaying()) {
						audioPlayer.reset();
					}

					audioPlayer.setDataSource(audioPlayer.getSongPathList().get(position));
					audioPlayer.prepare();
					audioPlayer.start();
					audioPlayer.setCurrentSongPosition(0);
					audioPlayer.setCurrentTrack(position);

				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

	}

}